Search this book | Previous | Table of contents | Next

Scripting WWW Browsers


The HyperCard stack, Scripting WWW Browsers version 0.01, exemplifies how you can send AppleEvents and AppleScripts to WWW browsers. It is a rudimentary stack intended to demonstrate the points made in the section below.


Example #1, Sending AppleEvents from HyperCard

Using an XCMD called SendAppleEvent (written by Donald Olson) it is possible to send AppleEvents to remote applications. It is available and documented in the stack at <URL:http://152.1.24.177/teaching/archives/SendAppleEvent-XCMD.hqx>.

The SendAppleEvent XCMD has the following syntax:

SendAppleEvent eventClass, eventID, targetProgram, replyRequest, interactionRequest, timeOutValue, data, keyword, data, keyword, data, keyword, data, keyword, data, keyword
where:
eventClass
An AEEventClass

eventID
An AEEventID

targetProgram
Path to target application in the form "zone:mac:application" if local, only "application" is required

replyRequest
noReply, queueReply, waitReply

interactionRequest
canInteract, neverInteract, alwaysInteract

timeOutValue
in ticks

data
TEXT

keyword
data Type (Descriptor type) If you are only sending one piece of data and it is to be the direct object, the first key is optional.

Using this command you can create a button sending an AppleEvent to MacWeb as shown below:

on mouseUp
  -- make sure MacWeb is open
  open "MacWeb"
  
  -- send the apple event necessary to to open a URL
  sendappleevent "wwwc", "ourl", "MacWeb", "noReply", "neverInteract","300","http://www.lib.ncsu.edu/","kURL",data2,key2,data3,key3,data4,key4,data5,key5
end mouseUp
(Unfortunately, this exact example does not work with MacMosaic nor Netscape.)

As you can see from the example, sending AppleEvents from HyperCard to applications can be a cryptic process! This is exactly why AppleScript was invented. AppleScript is a way of putting a more-human language on top of these cryptic AppleEvents.


Example #2, Sending AppleScript from HyperCard

Using HyperCard 2.2 or later, you can put AppleScripts into your HyperCard stacks. AppleScripts are much easier to work with than raw AppleEvents.

A developing standard AppleScript command, currently supported by MacMosiac and Netscape, is GetURL. Given a URL, this command enables you to tell your WWW browser to take the URL, retrieve the item is is associated with, and display it or save it to a file. Here is the syntax of the GetURL AppleScript command:

GetURL  string  -- The url 
  [to  file specification]  -- file the URL should be loaded into  
  [inside  reference]  -- Window the URL should be loaded to
  [from  string]  -- Refererer, to be sent with the HTTP request
Consequently, a button in your HyperCard stack (or a handler in an AppleScript) containing the following code will tell MacMosic open a connection to http://www.lib.ncsu.edu/:
tell application "MacMosaic"
  activate
  OpenURL "http://www.lib.ncsu.edu/"
end tell
Similarly, this code will tell Netscape to open up a connection to http://www.lib.ncsu.edu/staff/morgan/ and save the output to a file named myfile.html on your Macintosh's desktop:
tell application "Netscape"
  activate
  set outputFile to (path to desktop as string) & "myfile.html"
  GetURL "http://www.lib.ncsu.edu/staff/morgan/" to file outputFile
end tell
It should be noted that Netscape includes many more AppleScript command then just GetURL. For example, it includes a command called OpenURL which is a more full-featured version of GetURL. It also includes options to dynamically register MIME types, get the information about its open windows, and navigate back and forth between recently visited URLs.


Possible Uses

There are a number of possibilities for this sort of thing. For example you could create a database of URLs and include in the database the ability to view any of the URLs by selecting a function from the database application.

A simpler idea is to create an AppleScript with the following code:

set theURL to the clipboard
tell application "Netscape"
  activate
  GetURL theURL
end tell
This code simply takes the contents of the clipboard and puts it into a variable called "theURL". It then tells a WWW browser (Netscape) to open the URL. Saved as an AppleScript application that never shows its startup screen, you could save it in the Automated Tasks folder of your Apple Menu Folder. Once there you could select and copy URLs from any application (email, word processor, etc.) and open your newly created script. The script will then activate your WWW browser and open the URL.

Another, more elaborate application could be the creation of a simple expert system used to search the Internet. This application would ask you questions. Based on the answers it would ask you other questions. At the end of the question and answer process, the script would dynamically create an HTML page presenting the results of the question and answer process as well as listing a number of hot links to remote Internet resources that may have the information you seek.


Search this book | Previous | Table of contents | Next

Eric last edited this page on September 26, 1995. Please feel free to send comments.